
package Beans;

import javax.ejb.Stateless;
import Entity.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


/**
 *
 * @author Randall Chia
 */
@Stateless
public class StakeholderBillingBean implements StakeholderBillingBeanRemote {
    
    @PersistenceContext()
    EntityManager em;
    
    private Date previousBillingDate;
    private Date currentBillingDate;
    

    //This method creates a new instance of StakeholderBill for each stakeholder. This method
    //is intended to be called once on the billing day of each month
    public void billStakeholders(Date previousDate, Date currentDate){
        previousBillingDate = previousDate;
        currentBillingDate = currentDate;
        
        Query q1 = em.createQuery("SELECT c FROM Stakeholder c");
        
        for(Object o : q1.getResultList()){
            Stakeholder stkhldr = (Stakeholder)o;
            //TODO BILLING CODE
            
            //Make a new billing entity to hold the billing info
            StakeholderBill bill = new StakeholderBill();
            bill.create(stkhldr.getName(), currentBillingDate, 0.0, 0.0, 0.0, 0.0);
            
            
            //set the reinbursement (transaction revenue less commission)
            bill.setReInbursement(calculate_reinbursement(stkhldr.getName()));
            
            //set subscription fee
            bill.setBillSubscription(calculate_subscriptionFee(stkhldr.getName()));
            
            //set advertisement fee
            bill.setBillAdvertising(calculate_advertisingFee(stkhldr.getName()));
            
            //set the market intelligence fee (if a stakeholder is subscribed to market intelligence, he will be charged for it monthly)
            if(stkhldr.isPurchaseBI()){
                bill.setBillAdvertising(calculate_marketIntelligenceFee(stkhldr.getName()));
            }
            
            //get Subtotal
            bill.computeSubtotal();
            
            
            //em.persist(bill);
            
            
            
            stkhldr.addBill(bill);
            em.flush();
            
            System.out.println("StakeholderBillingBean: Create bill SUCCESS for stakeholder: " + stkhldr.getName());
            
            
        }  
    }//------------------------------------------------------------------------------------------
    
    private Double calculate_reinbursement(String stakeholderName){
        Double returnValue = 0.0;
        Query q1 = em.createQuery("SELECT c FROM PurchasedItem c WHERE c.seller = :paramstakeholder AND c.time_stamp > :paramPreviousBillDate");
        q1.setParameter("paramstakeholder", stakeholderName);
        q1.setParameter("paramPreviousBillDate", previousBillingDate); //filter out all transactions before the previous billing date
        
        for(Object o: q1.getResultList()){
            PurchasedItem item = (PurchasedItem) o;
            returnValue += item.getSubtotal();
        }
        System.out.println(">>> StakeholderBillingBean: Billing " + stakeholderName + " Reinbursement: " + returnValue);
        return returnValue;
    }//------------------------------------------------------------------------------------------
    
    private Double calculate_subscriptionFee(String stakeholderName){
        Stakeholder stkhldr = em.find(Stakeholder.class, stakeholderName);       
        System.out.println(">>> StakeholderBillingBean: Billing " + stakeholderName + " Subscription: " + stkhldr.getStakeholderTier().getFeeSubscription());
        return stkhldr.getStakeholderTier().getFeeSubscription();
    }//------------------------------------------------------------------------------------------
    
    private Double calculate_advertisingFee(String stakeholderName){
        
       //NOT YET IMPLEMENTED
        return 0.0;
    }//------------------------------------------------------------------------------------------
    
    private Double calculate_marketIntelligenceFee(String stakeholderName){
        Stakeholder stkhldr = em.find(Stakeholder.class, stakeholderName);  
        System.out.println(">>> StakeholderBillingBean: Billing " + stakeholderName + " MarketIntelligence: " + stkhldr.getStakeholderTier().getFeeMarketIntelligence());
        return stkhldr.getStakeholderTier().getFeeMarketIntelligence();
    }//------------------------------------------------------------------------------------------
    
    
}
